package com.mstore.service;

import java.util.ArrayList;
import java.util.List;

import com.mstore.dao.ShopDao;
import com.mstore.pojo.Shop;
/**
 * 
 * 企业分店信息
 *
 */
public class ShopService {
	
	private ShopDao shopDao;
	
	public void addShop(Shop shop){
		shop.setDelFlag(1);
		shopDao.save(shop);
	}
	
	public void deleteShop(Shop shop){
		shopDao.delete(shop);
	}
	
	public void updateShop(Shop shop){
		shop.setDelFlag(1);
		shopDao.update(shop);
	}
	/**
	 * 
	 * 获取单个分店信息
	 */
	public Object getOneShop(Shop shop){
		return shopDao.getOneShop(shop);
	}
	/**
	 * 
	 * 获取单个分店信息
	 */
	public Shop getShopById(Shop shop) {
		Object object = shopDao.getOneShop(shop);
		if (object == null) {
			return null;
		}
		return (Shop)object;
	}
	/**
	 * 获取企业下的所有分店
	 */
	public List<Shop>getAllShop(Shop shop){
		List<Object>objects = shopDao.getAllShop(shop);
		List<Shop>shops = new ArrayList<Shop>();
		for (Object object : objects) {
			Shop sp = (Shop)object;
			sp.setCreateTime(sp.getCreateTime().substring(0, sp.getCreateTime().indexOf(".")));
			shops.add(sp);
		}
		return shops;
	}
	/**
	 * 获取企业下的所有分店
	 */
	public List<Object>getAllShopObj(Shop shop){
		List<Object>objects = shopDao.getAllShop(shop);
		return objects;
	}
	
	/**
	 * 获取企业下的所有分店
	 */
	public List<Shop> getAllShopByEaId(Shop shop){
		List<Shop> shops = new ArrayList<Shop>();
		List<Object> objects = shopDao.getAllShop(shop);
		for (Object object : objects) {
			shops.add((Shop)object);
		}
		return shops;
	}
	
	/**
	 * 
	 * 会获取的企业所有分店信息进行分页
	 */
	public List<Object>getShopPage(int index,Shop shop){
		return shopDao.getShopPage(index, shop);
	}
	/**
	 * 
	 *更新分店地址
	 */
	public void updateAddress(Shop shop){
		shopDao.updateAddress(shop);
	}
	
	public Shop getShopByEaAndName(Long eaId, String shopName) {
		List<Object> objs = shopDao.getShopByEaAndName(eaId, shopName);
		if (!objs.isEmpty()) {
			return (Shop) objs.get(0);
		}
		return null;
	}
	
	public ShopDao getShopDao() {
		return shopDao;
	}
	public void setShopDao(ShopDao shopDao) {
		this.shopDao = shopDao;
	}
}
